#include <iostream>
#include <vector>
#include "../include/ulubatli.h"



SDL_Surface *screen = NULL;
SDL_Surface *sprite = NULL;
SDL_Surface *buffer = NULL;
SDL_Surface *tugla = NULL;

SDL_Rect koord[2];
std::vector<SDL_Surface*> vDuvar;
std::vector<SDL_Surface*>::iterator itDuvar;
Ulubatli *ulubatli;

void ciz();
void sil();
bool tuglaAlgila(Ulubatli* ulu,SDL_Surface* tugla,SDL_Rect tuglaKoord);
bool SDL_TransparentPixel2(SDL_Surface *surface , int u , int v);


int main(int argc, char* args[])
{

    if(SDL_Init(SDL_INIT_VIDEO) == -1)
        return 1;

    Uint8 *keystate;
    ulubatli = new Ulubatli();
    screen = SDL_SetVideoMode( 640, 480, 32, SDL_DOUBLEBUF|SDL_SWSURFACE);
//    screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF|SDL_FULLSCREEN|SDL_SWSURFACE);

    //Tuş basılı tutulduğunda işlevini tekrar tekrar gerçekleştirmesi için.
    SDL_EnableKeyRepeat(10, 10);
    SDL_WM_SetCaption("Ulubatlı", "Ulubatlı");


    tugla = IMG_Load("img/tugla.png");
    vDuvar.push_back(tugla);

    koord[0].x=ulubatli->getX();
    koord[0].y=ulubatli->getY();

    koord[1].x=300;
    koord[1].y=400;

    sprite = ulubatli->getSprite(2,1);
    ciz();

    SDL_Rect kare;
    kare.x=50;
    kare.y=50;
    kare.w=50;
    kare.h=50;

    bool durum = false;
    while(!durum)
    {
        SDL_Event event;
        if(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                    durum=true;
                    break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                        case SDLK_ESCAPE:
                            durum = true;
                            break;
                        case SDLK_LEFT:
                            for (int i=0;i<4;i++)
                            {
                                sil();
                                koord[0].x = ulubatli->getX();
                                koord[0].x -=2;

                                ulubatli->setX(koord[0].x);
                                if(!tuglaAlgila(ulubatli,vDuvar.at(0),koord[1]))
                                {
                                    SDL_FillRect(screen,&kare,SDL_MapRGB(screen->format,255,0,0));
                                }
                                else
                                    SDL_FillRect(screen,&kare,SDL_MapRGB(screen->format,0,255,0));

                                if (i==0 || i==3)
                                    sprite = ulubatli->getSprite(1,1);
                                else if (i==1)
                                    sprite = ulubatli->getSprite(1,0);
                                else
                                    sprite = ulubatli->getSprite(1,2);

                                ciz();
                            }
                            break;
                        case SDLK_RIGHT:
                            for (int i=0;i<4;i++)
                            {
                                sil();
                                int x;
                                x = ulubatli->getX();
                                x +=2;
                                ulubatli->setX(x);
                                if(!tuglaAlgila(ulubatli,vDuvar.at(0),koord[1]))
                                {
                                    SDL_FillRect(screen,&kare,SDL_MapRGB(screen->format,255,0,0));
                                }
                                else
                                    SDL_FillRect(screen,&kare,SDL_MapRGB(screen->format,0,255,0));


                                if (i==0 || i==3)
                                    sprite = ulubatli->getSprite(0,1);
                                else if (i==1)
                                    sprite = ulubatli->getSprite(0,0);
                                else
                                    sprite = ulubatli->getSprite(0,2);

                                ciz();
                            }
                            break;
                    }
                    break;
            }
        }

        keystate = SDL_GetKeyState(NULL);
//
//        if (keystate[SDLK_LEFT])
//        {
//            for (int i=0;i<4;i++)
//            {
//                sil();
//                koord[0].x = ulubatli->getX();
//
//                    koord[0].x -=2;
//
//                ulubatli->setX(koord[0].x);
//
//                if (i==0 || i==3)
//                    sprite = ulubatli->getSprite(1,1);
//                else if (i==1)
//                    sprite = ulubatli->getSprite(1,0);
//                else
//                    sprite = ulubatli->getSprite(1,2);
//
//
//                ciz();
//            }
//        }
//        if (keystate[SDLK_RIGHT])
//        {
//            for (int i=0;i<4;i++)
//            {
//                sil();
//                koord[0].x = ulubatli->getX();
//
//                    koord[0].x +=2;
//
//                ulubatli->setX(koord[0].x);
//
//                if (i==0 || i==3)
//                    sprite = ulubatli->getSprite(0,1);
//                else if (i==1)
//                    sprite = ulubatli->getSprite(0,0);
//                else
//                    sprite = ulubatli->getSprite(0,2);
//
//                ciz();
//            }
//        }
        if (keystate[SDLK_DOWN])
        {
            for (int i=0;i<4;i++)
            {
                sil();
                int y;
                y = ulubatli->getY();
                y +=2;
                ulubatli->setY(y);
                if(!tuglaAlgila(ulubatli,vDuvar.at(0),koord[1]))
                {
                    SDL_FillRect(screen,&kare,SDL_MapRGB(screen->format,255,0,0));
                }
                else
                    SDL_FillRect(screen,&kare,SDL_MapRGB(screen->format,0,255,0));

                if (i==0 || i==3)
                    sprite = ulubatli->getSprite(2,1);
                else if (i==1)
                    sprite = ulubatli->getSprite(2,0);
                else
                    sprite = ulubatli->getSprite(2,2);

                ciz();
            }
        }
        if (keystate[SDLK_UP])
        {
            for (int i=0;i<4;i++)
            {
                sil();
                int y;
                y = ulubatli->getY();
                y -=2;
                ulubatli->setY(y);
                if(!tuglaAlgila(ulubatli,vDuvar.at(0),koord[1]))
                {
                    SDL_FillRect(screen,&kare,SDL_MapRGB(screen->format,255,0,0));
                }
                else
                    SDL_FillRect(screen,&kare,SDL_MapRGB(screen->format,0,255,0));

                if (i==0 || i==3)
                    sprite = ulubatli->getSprite(3,1);
                else if (i==1)
                    sprite = ulubatli->getSprite(3,0);
                else
                    sprite = ulubatli->getSprite(3,2);


                ciz();
            }
        }
    }

    SDL_FreeSurface(sprite);
    SDL_FreeSurface(screen);
    SDL_FreeSurface(buffer);

    SDL_Quit();

    return 0;
}

void
ciz()
{
    SDL_BlitSurface(sprite,NULL,screen,&ulubatli->getKoord());
    SDL_BlitSurface(vDuvar.at(0),NULL,screen,&koord[1]);
    SDL_Flip(screen);
    SDL_Delay(20);
}

void
sil()
{
    SDL_FillRect(screen,NULL,0);
}

bool
SDL_TransparentPixel(SDL_Surface *surface , int x , int y)
{
	int bpp = surface->format->BytesPerPixel;
	Uint8* p = (Uint8*)surface->pixels + y * surface->pitch + x * bpp;
	Uint32 pixelColor;

	switch(bpp)
	{
		case(1):
			pixelColor = *p;
			break;
		case(2):
			pixelColor = *(Uint16*)p;
			break;
		case(3):
			if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
				pixelColor = p[0] << 16 | p[1] << 8 | p[2];
			else
				pixelColor = p[0] | p[1] << 8 | p[2] << 16;
			break;
		case(4):
			pixelColor = *(Uint32*)p;
			break;
	}

	Uint8 red, green, blue, alpha;
	SDL_GetRGBA(pixelColor, surface->format, &red, &green, &blue, &alpha);

	return alpha > 200;
}


bool
tuglaAlgila(Ulubatli* ulu,SDL_Surface* tugla,SDL_Rect tuglaKoord)
{
    int tuglaLeft = tuglaKoord.x;
    int tuglaRight = tugla->w + tuglaKoord.x;
    int tuglaTop = tuglaKoord.y;
    int tuglaBottom= tugla->h + tuglaKoord.y;

    if (ulu->getRight()< tuglaLeft || tuglaRight < ulu->getLeft())
        return false;
    if (ulu->getBottom()< tuglaTop || tuglaBottom < ulu->getTop())
        return false;

    int left = ulu->getLeft()>tuglaLeft ? ulu->getLeft() : tuglaLeft;
    int top = ulu->getTop()>tuglaTop ? ulu->getTop() : tuglaTop;
    int right = ulu->getRight()<tuglaRight ? ulu->getRight() : tuglaRight;
    int bottom = ulu->getBottom()< tuglaBottom ? ulu->getBottom() : tuglaBottom;

    int width = right - left;
    int height = bottom - top;

    if (width <=0 || height<=0)
        return false;

    for (int y=0;y<=height;y++)
    {
        for (int x=0;x<=width;x++)
        {
            if(SDL_TransparentPixel(ulu->getMevcutSprite(),x+(ulu->getLeft()),y+(ulu->getTop())) &&
               SDL_TransparentPixel(tugla,x+tuglaLeft,y+tuglaTop))
               return true;
        }
    }

}
//bool
//collision(SDL_Surface *as,int ax , int ay , SDL_Surface *bs , int bx , int by)
//{
//	/*Box A;
//	Box B;*/
//
//	/*a - bottom right co-ordinates*/
//	int ax1 = ax + as->w - 1;
//	int ay1 = ay + as->h - 1;
//
//	/*b - bottom right co-ordinates*/
//	int bx1 = bx + bs->w - 1;
//	int by1 = by + bs->h - 1;
//
//	/*check if bounding boxes intersect*/
//	if((bx1 < ax) || (ax1 < bx))
//		return 0;
//	if((by1 < ay) || (ay1 < by))
//		return 0;
//
//
///*Now lets make the bouding box for which we check for a pixel collision*/
//
//	/*To get the bounding box we do
//	    Ax1,Ay1_____________
//		|		|
//		|		|
//		|		|
//		|    Bx1,By1_____________
//		|	|	|	|
//		|	|	|	|
//		|_______|_______|	|
//			|    Ax2,Ay2	|
//			|		|
//			|		|
//			|____________Bx2,By2
//
//	To find that overlap we find the biggest left hand cordinate
//	AND the smallest right hand co-ordinate
//
//	To find it for y we do the biggest top y value
//	AND the smallest bottom y value
//
//	Therefore the overlap here is Bx1,By1 --> Ax2,Ay2
//
//	Remember	Ax2 = Ax1 + SA->w
//			Bx2 = Bx1 + SB->w
//
//			Ay2 = Ay1 + SA->h
//			By2 = By1 + SB->h
//	*/
//
//	/*now we loop round every pixel in area of
//	intersection
//		if 2 pixels alpha values on 2 surfaces at the
//		same place != 0 then we have a collision*/
//	int inter_x0 = SDL_COLLIDE_MAX(ax,bx);
//	int inter_x1 = SDL_COLLIDE_MIN(ax1,bx1);
//
//	int inter_y0 = SDL_COLLIDE_MAX(ay,by);
//	int inter_y1 = SDL_COLLIDE_MIN(ay1,by1);
//
//	for(int y = inter_y0 ; y <= inter_y1 ; y++)
//	{
//		for(int x = inter_x0 ; x <= inter_x1 ; x++)
//		{
//			if((SDL_TransparentPixel(as , x-ax , y-ay))/*compute offsets for surface*/
//			&& (SDL_TransparentPixel(bs , x-bx , y-by)))/*before pass to SDL_TransparentPixel*/
//				return 1;
//		}
//	}
//}
